<?php
/**
 * @description : show the message on static html files.
 * @author      : stephen.mo <stephen.mo@achievo.com>
 * @date        : Dec 14,2010
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("display_errors","on");
$action           = (isset($_REQUEST['act'])) ? $_REQUEST['act'] : NULL;
$lang             = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$flag             = (isset($_REQUEST['flag'])) ? $_REQUEST['flag'] : OAuthModelConfig::DEFAULT_CONST_FAILURE;

//setting paths.
$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER); 
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;
$form             = array();
$mess_page_info   = OPI::$other_page_info[$lang];

//do with diff type of actions.
$action = strtolower($action);
switch($action) {
    case 'fpass'://forget password page.
        $fpass_page_info = OPI::$forget_password[$lang];
        $message         = array('succ_title'=>$fpass_page_info['fpassword_succ_title'],'succ_msg'=>$fpass_page_info['fpassword_succ_msg']);
        break;
    case 'reg'://register page.
        $reg_page_info   = OPI::$register_page_info[$lang];
        $message         = array('succ_title'=>$reg_page_info['reg_succ_title'],'succ_msg'=>$reg_page_info['reg_succ_msg'],'succ_btn'=>$reg_page_info['reg_succ_btn']);
        break;
    case 'request':
        //$req_page_info  
        $name              = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : 'h3g';
        $request_page_info = OPI::$request_page_info[$lang][$name];
        $payment_buttons   = OPI::$payment_button_info[$lang];
        $form['name']      = $name;
        if ($flag == OAuthModelConfig::DEFAULT_CONST_FALIURE) {
            $message['msg_title'] = $request_page_info['request_err_title'];
            $err_ids              = (isset($_REQUEST['errids'])) ? json_decode($_REQUEST['errids']) : NULL;
            $error_codes          = OPI::$errorCodeTxt[$lang];
            foreach((array)$err_ids as $val) {
                $message['msg_list'][] = $error_codes[$val];
            }
            
            //setting button.
            $retry_url = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL; 
            $message['msg_btn'] = array('retry'=>array('value'=>$payment_buttons['retry'],'link'=>$retry_url));

        }
        else {
            $message['msg_title'] = $request_page_info['request_suc_title'];
        }
        break;
    case 'pay':
        //fail 
        if ($flag == OAuthModelConfig::DEFAULT_CONST_FALIURE) { 
            $error_page_info        = OPI::$error_page_info[$lang];
            $payment_buttons        = OPI::$payment_button_info[$lang];
            $message['error_title'] = $error_page_info['payment_title'];
            $err_ids                = (isset($_REQUEST['errids'])) ? json_decode($_REQUEST['errids']) : NULL;
            $error_codes            = OPI::$errorCodeTxt[$lang];
            foreach((array)$err_ids as $val) {
                $message['msg_list'][] = $error_codes[$val];
            }
            $retry_url  = (isset($_REQUEST['call_back']) && !empty($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : ((isset($_REQUEST['reback_url']) && !empty($_REQUEST['reback_url'])) ? $_REQUEST['reback_url'] : NULL);
            $message['msg_btn']     = array('retry'=>array('value'=>$payment_buttons['retry'],'link'=>$retry_url));
        }
        elseif ($flag == OAuthModelConfig::DEFAULT_CONST_SUCCESS) {
            $page_info  = OPI::$other_page_info[$lang];
            $call_back  = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
            $order_ref  = (isset($_REQUEST['order_ref'])) ? $_REQUEST['order_ref'] : NULL;
            $period_txt = (isset($_REQUEST['period'])) ? " '".base64_decode($_REQUEST['period'])."' " : NULL;
            
            $message['success_title'] = $page_info['msg_payment_title'];
            $message['success_list']  = str_replace('{%SEVICE%}',$period_txt,$page_info['msg_succ_payment']);
            $message['orderRef']      = $order_ref;
            $message['order_title']   = $page_info['msg_order_title'];
            $message['order_end']     = $page_info['msg_payment_end'];
        }

        break;
    case 'finish':
        $name                 = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : 'h3g';
        $request_page_info    = OPI::$request_page_info[$lang][$name];
        $form['name']         = $name;
        $message['msg_title'] = OPI::$other_page_info[$lang]['msg_request_error'];
        $message['msg_note']  = $request_page_info['request_suc_finish'];
        break;
    case 'exists':
        $name                 = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : 'h3g';
        $request_page_info    = OPI::$request_page_info[$lang][$name];
        $form['name']         = $name;
        $message['msg_title'] = OPI::$other_page_info[$lang]['msg_request_error'];
        $message['msg_note']  = $request_page_info['request_suc_exists'];
        break;
    case 'prompt':
        $prompt_page_info        = OPI::$error_page_info[$lang];
        $payment_buttons         = OPI::$payment_button_info[$lang];
        $message['prompt_title'] = $prompt_page_info['prompt_title'];
        $err_ids                 = (isset($_REQUEST['errids'])) ? json_decode($_REQUEST['errids']) : NULL;
        $error_codes             = OPI::$errorCodeTxt[$lang];
        $only_confirm_btn        = FALSE;
        foreach((array)$err_ids as $val) {
            $special_txt = NULL; 
            switch($val) {
                case 1031:
                    $tmp_txt  = $error_codes[$val];
                    $login_id = (isset($_REQUEST['login_id'])) ? OAuthUtils::getDefaultLoginId($_REQUEST['login_id']) : NULL;
                    if (!empty($login_id)) {
                        $userPkgInfo = OAuthModel::getInstance()->getUserCurrentPlan($login_id,$lang);
                        if ($userPkgInfo) {
                            $special_txt = str_replace('{%SERVICE_TITLE%}','<em>'.$userPkgInfo['title'].'</em>',$tmp_txt); 
                        }
                    }
                    else {
                        $special_txt = $error_codes[1036];
                    }
                    $only_confirm_btn = TRUE;
                    break;
                default:
            }
            $message['msg_list'][] = (empty($special_txt)) ? $error_codes[$val] : $special_txt;
        }
        
        //buttons setting.
        $retry_url = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
        $message['ajax'] = NULL;
        if ($only_confirm_btn) {
            $call_back_str      = '&call_back='.urlencode($retry_url);
            $reback_url_str     = '&reback_url='.urlencode($retry_url);
            $lang_str           = (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) ? '&lang='.OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
            $oauth_token_str    = '&oauth_token=';
            $message['ajax']    = OAuthConfig::OAuthDomainUrl($_SERVER).OAuthConfig::DEFAULT_AJAX_URL.'?item=logout'.$call_back_str.$reback_url_str.$lang_str.$oauth_token_str;
            $message['msg_btn'] = array('confrim'=>array('value'=>$payment_buttons['logout'],'link'=>$retry_url));
        }
        else {
            $message['msg_btn'] = array('retry'=>array('value'=>$payment_buttons['retry'],'link'=>$retry_url));
        }
        break;
    case 'error':
        $error_page_info        = OPI::$error_page_info[$lang];
        $payment_buttons        = OPI::$payment_button_info[$lang];
        $message['error_title'] = $error_page_info['error_title'];
        $err_ids                = (isset($_REQUEST['errids'])) ? json_decode($_REQUEST['errids']) : NULL;
        $error_codes            = OPI::$errorCodeTxt[$lang];
        foreach((array)$err_ids as $val) {
            $message['msg_list'][] = $error_codes[$val];
        }
        $retry_url              = (isset($_REQUEST['call_back']) && !empty($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : ((isset($_REQUEST['retry_url']) && !empty($_REQUEST['retry_url'])) ? $_REQUEST['retry_url'] : NULL);
        $message['msg_btn']     = array('retry'=>array('value'=>$payment_buttons['retry'],'link'=>$retry_url));
        break;
    case 'agree':
        //message for agreement text.
        $message = NULL;
        break;
    default: //error message.
        $action = 'error';
        $error_page_info = OPI::$other_page_info[$lang];
        $message         = array('error_title'=>$error_page_info['error_title'],'error_msg'=>$error_page_info['error_msg']);
}

//loading template.
$using_real_env = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page           = OAuthModelConfig::M_PAGE_OF_MESSAGE;
$form['lang']   = $lang;
$form['title']  = $mess_page_info['page_title'];
$form['act']    = $action;
$form['flag']   = $flag;
$form['msgbox'] = $message;

//include html page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
